package ferri.lorenzo;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ArtistListActivity extends ListActivity {
	
	
	private Cursor managedCursor;			
	
	
	private String[] path = null;			//Songs Path
	private String[] tit = null;			//Songs Title
	private String[] alb = null;			//Albums Title
	private String[] art = null;			//Artists
	private String[] dur = null;			//Songs duration
	private long[] albart = null;			//ID Albums List

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		String query = "%mp3";

		// Create query to extract mp3 file information
		
		String sel = MediaStore.Audio.Media.ARTIST;
		managedCursor = managedQuery(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { 									// Audio MediaStore
				MediaStore.Audio.Media._ID, 					//ID Song
				MediaStore.Audio.Media.ARTIST, 					// Artists Name
				MediaStore.Audio.Media.ARTIST_ID, 				// Artists ID
				MediaStore.Audio.Media.DISPLAY_NAME 			//Complete File Name
				},
				MediaStore.Audio.Media.IS_MUSIC + "=1" + " AND "
				+ MediaStore.Audio.Media.DISPLAY_NAME + " LIKE \""
				+ query + "\"" + ") GROUP BY (" + sel, 			// CLAUSE WHERE 
				null, 											// WHERE parameter
				MediaStore.Audio.Media.ARTIST 					// ORDER BY
		);

		ListAdapter adapter = new SimpleCursorAdapter(this, // context
				android.R.layout.simple_list_item_1, 		// layout
				managedCursor, 								// cursor
				new String[] { 								// from(columns)
				MediaStore.Audio.Media.ARTIST 				// artists
				}, new int[] { 								// to(textids)
				android.R.id.text1, });
		setListAdapter(adapter);
	}

	//Selection song event click
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);


		managedCursor.moveToPosition(position);
		
		long artistId = managedCursor.getLong(managedCursor
				.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));

		//Create Intent to send information
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.putExtra(MediaStore.Audio.Media.ARTIST_ID, artistId);
		intent.setClass(ArtistListActivity.this, AlbumListActivity.class);

		// Start activity and attend the result
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// You can use the requestCode to select between multiple child
		// activities you may have started. Here there is only one thing
		// we launch.
		if (requestCode == 0) {

			// We will be adding to our text.

			// This is a standard resultCode that is sent back if the
			// activity doesn't supply an explicit result. It will also
			// be returned if the activity failed to launch.
			if (resultCode != RESULT_CANCELED) {
				if (data != null) {

					path = data.getStringArrayExtra("path");
					tit = data.getStringArrayExtra("titolo");
					dur = data.getStringArrayExtra("durata");
					alb = data.getStringArrayExtra("album");
					art = data.getStringArrayExtra("artist");
					albart = data.getLongArrayExtra("art");

				}
			}
		}

	}

	//Method used to insert informations about songs selected, in the intent
	public void OK(View view) {

		switch (view.getId()) {
		case R.id.button_ok:

			Intent intent = new Intent(Intent.ACTION_VIEW);

			intent.putExtra("titolo", tit); 	
			intent.putExtra("album", alb);
			intent.putExtra("artist", art);
			intent.putExtra("durata", dur);
			intent.putExtra("path", path);
			intent.putExtra("art", albart);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
}